# encoding = "GB2312"
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']
from matplotlib import pyplot as plt
import pandas as pd
import numpy as np

def Draw_meth_slelec(method,data_list,data_name_list):
    if method == 1:
        Continuous_draw(data_list,data_name_list)
    if method == 2:
        Subgraph_draw(data_list,data_name_list)

# 连续绘制4个图形
def Continuous_draw(data_list,data_name_list):
    name_num = 1
    for each in data_list :
        fig = plt.figure(figsize=(20,8),dpi = 80)
        
        _xtick_labels = list(each.index)
        _ytick_labels = list(each.values)
        
        _x = []
        for i in _xtick_labels :
            _x.append("%s代"%i)
        
        plt.plot(_xtick_labels,_ytick_labels,label = data_name_list[name_num]+ "变化情况")
        plt.legend(loc = 0,fontsize=20) 
        
        plt.xticks(_xtick_labels[0:][::50],_x[0:][::50],rotation =45,fontsize=20)
        y_ranges(name_num,_ytick_labels)

        plt.title(data_name_list[0] + "-" + data_name_list[name_num],fontsize=30)
        name_num += 1

        plt.xlabel("代数",fontsize=30)
        plt.grid(alpha = 0.6,axis="y",linestyle="--",linewidth=0.5)
        plt.show()
        print("生成成功")

def Subgraph_draw(data_list,data_name_list):
    fig = plt.figure(figsize=(40,16),dpi = 80)
    name_num = 1
    for each in data_list :
        plt.subplot(2,2,name_num)

        _xtick_labels = list(each.index)
        _ytick_labels = list(each.values)

        _x = []
        for i in _xtick_labels :
            _x.append("%s代"%i)

        plt.plot(_xtick_labels,_ytick_labels,label = data_name_list[name_num]+ "变化情况")
        plt.legend(loc = 0,fontsize=8) 

        plt.xticks(_xtick_labels[0:][::50],_x[0:][::50],rotation =45,fontsize=8)
        y_ranges(name_num,_ytick_labels)

        plt.gca().set_title(data_name_list[0] + "-" + data_name_list[name_num],fontsize=20)
        name_num += 1

        plt.grid(alpha = 0.6,axis="y",linestyle="--",linewidth=0.5)
        print("生成成功")
    plt.show()

# 不同类型的值对应的y的取值范围
def y_ranges(name_num,values):
    if name_num == 1 :
        y_range =  plt.yticks(np.arange(int(min(values)),int(max(values)+2),1),fontsize=20)
        return y_range
    elif name_num == 1 :
        y_range =  plt.yticks(np.arange(int(min(values)),int(max(values)+2),1),fontsize=20)
        return y_range
    elif name_num == 1 :
        y_range =  plt.yticks(np.arange(int(min(values)),int(max(values)+2),1),fontsize=20)
        return y_range
    elif name_num == 1 :
        y_range =  plt.yticks(np.arange(min(values)-0.1,max(values)+0.1,0.1),fontsize=20)
        return y_range

def main():
    df = pd.read_csv("D:/hello Python/邓老师任务/test4/stat.csv",encoding = "GB2312")
    # print(df.head())
    # print(df.info())
    optimal_value = df.iloc[:,2]
    worst_value = df.iloc[:,3]
    average_fitness = df.iloc[:,5]
    variance_fitness = df.iloc[:,6]
    data_list = [optimal_value,worst_value,average_fitness,variance_fitness]
    data_name_list= ["进化代数","种群最优适应值","种群最坏适应值","种群平均适应值","种群适应值方差"]
    # print(data_list)
    Draw_meth_slelec(1,data_list,data_name_list)  # 第一个参数可以选填1或2，实现控制方法的选择
if __name__ == '__main__':
    main()
    
